package com.code.leetcode._202503;

import java.util.HashMap;
import java.util.Map;

public class NoRepeatMaxString {
    public static void main(String[] args) {
        String str = "pwwkewkewp";
        System.out.println(lengthOfLongestSubstring1(str));
    }
    /**给定一个字符串，请你找出其中不含有重复字符的最长字串的长度**/
    public static int lengthOfLongestSubstring(String s) {
        Map<Character,Integer> map = new HashMap<>();
        int ans = 0;
        for (int start = 0,end = 0; end < s.length(); end++) {
            char right = s.charAt(end);
            map.put(right,map.getOrDefault(right,0)+1);
            while(map.get(right)>1){
                char left = s.charAt(start);
                map.put(left,map.get(left)-1);
                start++;
            }
            ans = Math.max(ans,end-start+1);
        }
        return ans;
    }
    public static int lengthOfLongestSubstring1(String s) {
        Map<Character, Integer> dic = new HashMap<>();
        int i = -1, res = 0, len = s.length();
        for(int j = 0; j < len; j++) {
            if (dic.containsKey(s.charAt(j)))
                i = Math.max(i, dic.get(s.charAt(j))); // 更新左指针 i
            dic.put(s.charAt(j), j); // 哈希表记录
            res = Math.max(res, j - i); // 更新结果
        }
        return res;
    }


}